import json
import logging,app
from logging import handlers

import pymysql
from bs4 import BeautifulSoup


def init_log_config():
    #1创建日志器
    logger=logging.getLogger()
    logger.setLevel(logging.INFO)

    #2创建控制台处理和文件处理器
    sh=logging.StreamHandler()
    log_path=app.BASC_DIR+"/log/p2p.log"
    fh=logging.handlers.TimedRotatingFileHandler(filename=log_path,when="M",interval=5,backupCount=5,encoding="utf-8")

    #3创建格式化器
    f="%(asctime)s %(levelname)s [%(filename)s(%(funcName)s:%(lineno)d)] - %(message)s"
    formatter=logging.Formatter(f)

    #4将格式化器添加到控制器处理器和文件处理器
    sh.setFormatter(formatter)
    fh.setFormatter(formatter)

    #5将处理器添加到日志器
    logger.addHandler(sh)
    logger.addHandler(fh)

class DB_Quid():
    conn=None
    @classmethod
    def __get_conn(cls):
        if cls.conn is None:
            cls.conn=pymysql.Connect(host='user-p2p-test.itheima.net',port=3306,user='root',password='Itcast_p2p_20191228',database='czbk_member',charset='utf8')
        return cls.conn
    @classmethod
    def __close_conn(cls):
        if cls.conn:
            cls.conn.close()
        cls.conn=None
    def db_quid(self,sql):
        cursor=None
        res=None
        try:
            cursor=self.__get_conn().cursor()
            if sql.split(' ')[0].lower() =='select':
                cursor.execute(sql)
                res=cursor.fetchall()
            else:
                cursor.execute(sql)
                res=cursor.rowcount
                self.conn.commit()
        except Exception as e:
            print('执行sql语句出错{}'.format(e))
            self.conn.rollback()
        finally:
            cursor.close()
            self.__close_conn()
            return res
def assert_untill(self,resp,status_code,status,description):
    self.assertEqual(status_code, resp.status_code)
    self.assertEqual(status, resp.json().get('status'))
    self.assertEqual(description, resp.json().get('description'))

def json_read():
    file_path=app.BASC_DIR+"/data/json_data.json"
    with open(file_path,encoding='utf-8') as f:
        dict_data=json.load(f)
        list_data=[]
        for i in dict_data:
            list_data.append(tuple(i.values()))
        return list_data

def get_third_rquest_method(self,form_data):
    soup = BeautifulSoup(form_data, 'html.parser')
    # 提取第三方开户url
    third_url = soup.form.get('action')
    dict_data = {}
    for i in soup.find_all('input'):
        dict_data[i.get('name')] = i.get('value')
    res = self.session.post(third_url, data=dict_data)
    return res


if __name__ == '__main__':
    sql="SELECT count(*) from czbk_finance.fn_tender f INNER JOIN czbk_member.mb_member m on f.member_id=m.id where m.phone='12012345678' and f.`status`='-2'"
    print(DB_Quid().db_quid(sql))
    a='abcdefghijk'





